package com.kuang.login.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtils {

    /**
     * 读取classpath目录下文件内容的API
     * @param filename
     * @return
     * @throws IOException
     */
    public static String readFile(String filename) throws IOException {
        String file = FileUtils.class.getClassLoader().getResource(filename).getFile();
        BufferedReader reader = new BufferedReader(new FileReader(file));
        return reader.readLine();
    }

    /**
     * 向classpath目录下文件名为filename的文件写入content
     * @param filename
     * @param content
     * @throws IOException
     */
    public static void writeFile(String filename, String content) throws IOException {
        String file = FileUtils.class.getClassLoader().getResource(filename).getFile();
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(content);
        fileWriter.flush();
    }
}
